<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/26
 * Time: 20:20
 */


$dindex=2;
$index=3;
$dapai=10;
$dcards=array( array(0=>15),array(1=>11),array(2=>11),array(3=>16),array(0=>12),array(1=>10),array(2=>10)); //,array(3=>10)
$f=is_updown_penghu($dcards,$dapai,$index);
function is_updown_penghu($dcards,$dapai,$index){
    $dcards=array_reverse($dcards,false);
    print_r($dcards);
    $num=0;
    foreach($dcards as $k=>$v){
        $data[]=$v;
        if(array_key_exists($index,$v)) break;
    }
    foreach ( $data as $item) {
        $item=array_values($item)[0];
        if($item==$dapai) $num++;
    }
    if($num>=2) return false;
    return true;
}
var_dump($f);
exit();

//
//$postData=[
//    'account_id'=>5840,
//    'operation'=>'CreateRoom',
//    'data'=>[
//        'ticket_type'=>1,
//        'game_type'=>1,
//    ],
//];
//
//
//$postData=[
//    'account_id'=>5841,//会员id
//    'operation'=>'PrepareJoinRoom',
//    'data'=>['room_number'=>'1b1ori9n2ao'],//房间号
//];
//
//
//
//
//
//$postData=[
//    'account_id'=>5840,//会员账号
//    'operation'=>'SitDown',
//    'data'=>[
//        'room_id'=>375587,//房间id
//        'sort'=>1//座位 （1  2  3  4）
//    ]
//];
//
//
//$postData=[
//    'account_id'=>5840,//会员账号
//    'operation'=>'GameStart',
//    'data'=>[
//        'room_id'=>375608,//房间id
//    ]
//];
//print_r(json_encode($postData));
//exit();
//
//$postData=[
//    'room_id'=>375602,  //房间号
//    'operation'=>'ShowCard',  //牌操作
//    'action'=>6,           //  暗杠
//    'turn'=>1,                //  当前转向
//    'data'=>[
//        'index'=>1,//摸牌者id
//        'indexdata'=>array( 3,3,3,6,8,10,16,18,23,24,26,27,35,3),//摸牌者的所有牌
//        'ppai'=>array(3,3,3,3)
//    ]
//];
//
//
//$postData=[
//    'room_id'=>375576,
//    'operation'=>'ShowCard',
//    'action'=>2,
//    'turn'=>0,
//    'data'=>[
//        'index'=>0,//打牌者id
//        'indexdata'=>[5, 7, 8, 10, 12, 14, 14, 16, 20, 21, 22, 26, 33, 35],//打牌者的所有牌
//        'dpai'=>5
//    ]
//];



//error_reporting(0);

//$cards = [ 5,5,5,8,6,6,7,7];
//$cards = [ 5,5,5,5,6,7,7,7];
//$cards = [ 1,2,3,3,4,5,7,7];
//$cards = [ 1,1,2,2,3,3,7,7];
//$cards = [ 1,1,1,3,3,3,7,7];
//$cards = [ 2,3,4,4,5,6,4,5,6,6,6];
//$cards = [ 0,2,1,4,5,6,4,5,6,6,6];
//$cards = [ 1,2, 3,3,3,3, 4,5,6,22, 23,24,15,15];
//$cards = [ 0,0,8,8,2,2,4,5,5,6,6,4,7,7];
//$cards = [ 1,2, 3, 4,4,3,3,3];
//$f=isqidui($cards);


?>


<?php
$hutype=array(0=>'平胡', 1=>'边倒', 2=>'对倒', 3=>'嵌倒', 4=>'单吊', 5=>'大吊', 6=>'对对胡', 7=>'杠开', 8=>'送杠', 9=>'拉杠', 10=>'天胡', 11=>'地胡', 12=>'清一色', 13=>'混一色', 14=>'字一色', 15=>'海底捞月', 16=>'八花', 17=>'四花', 18=>'中发白', 19=>'正风', 20=>'圈风', 21=>'正花', 22=>'野花', 22=>'门清', 23=>'自摸', 24=>'圈正风', 25=>'还杠', 26=>'三包', 27=>'互包');
$hutypescore=array(0=>1, 1=>1, 2=>1, 3=>1, 4=>1, 5=>8, 6=>8, 7=>1, 8=>1, 9=>1, 10=>13, 11=>13, 12=>10, 13=>8, 14=>10, 15=>1, 16=>16, 17=>8, 18=>1, 19=>1, 20=>1, 21=>2, 22=>1, 22=>1, 23=>1, 24=>2, 25=>5, 26=>[5,4], 27=>[10,8]);
/**
 * Created by PhpStorm.
 * User: Mz
 * Date: 2017/12/26
 * Time: 13:08
 * 定义游戏规则配置数据格式如下
 */
class Xiangshan{
    /*
    * @var array|           初始化麻将数据
    * $cards['yuhuapai']    剩余的花牌
    * $cards['yupai']       剩余的牌
    * $cards['disyupai']    剩余的扣除下来的牌

    */
    private $cards = array();
    /*
     * $playcards[0]['pai']         手上有效的牌
     * $playcards[0]['huapai']      手上的花牌
     * $playcards[0]['agangpai']    手上的暗杠牌
     * $playcards[0]['mgangpai']    手上的明杠牌
     * $playcards[0]['wgangpai']    手上的弯杠牌
     * $playcards[0]['pengpai']     手上的碰牌
     * $playcards[0]['chipai']      手上的吃牌
     * $playcards[0]['dapai']       打出去的牌
     * $playcards[0]['dispai']      不能打的牌，吃或者刚之后某些牌不能打
     * $playcards[0]['tuoguan']     1：代表已经托管，0：代表手动出牌
     * $playcards[0]['opengang']     1：代表翻开，0：代表不翻开
     */
    private  $playcards = array(
        array('pai'=>array(),'huapai'=>array(),'agangpai'=>array(),'mgangpai'=>array(),'wgangpai'=>array(),'pengpai'=>array(),'chipai'=>array(),'mopai'=>array(),'dapai'=>array(),'dispai'=>array(),'tuoguan'=>0,'opengang'=>0),
        array('pai'=>array(),'huapai'=>array(),'agangpai'=>array(),'mgangpai'=>array(),'wgangpai'=>array(),'pengpai'=>array(),'chipai'=>array(),'mopai'=>array(),'dapai'=>array(),'dispai'=>array(),'tuoguan'=>0,'opengang'=>0),
        array('pai'=>array(),'huapai'=>array(),'agangpai'=>array(),'mgangpai'=>array(),'wgangpai'=>array(),'pengpai'=>array(),'chipai'=>array(),'mopai'=>array(),'dapai'=>array(),'dispai'=>array(),'tuoguan'=>0,'opengang'=>0),
        array('pai'=>array(),'huapai'=>array(),'agangpai'=>array(),'mgangpai'=>array(),'wgangpai'=>array(),'pengpai'=>array(),'chipai'=>array(),'mopai'=>array(),'dapai'=>array(),'dispai'=>array(),'tuoguan'=>0,'opengang'=>0),
    );
    //庄家始终是0， 1，后面会涉及到换庄,
    public  $zhuang=array(0=>1,1=>1,2=>1,3=>1);
    //当前转向，一直循环下去，0代表东家摸牌打牌，1代表南家摸牌打牌，2代表西家摸牌打牌，3代表北家摸牌打牌
    private  $turn  =0;
    //打牌流水数据，方便查找到吃碰杠胡
    private  $dcards  =[];
    //打的第几场
    private  $num=0;
    //倒计时
    private  $timer  =10;

    //一炮双响倒计时(如果第一家已经点了，如果等待10秒，其他玩家还没有点击胡牌，此牌局直接结束)
    private  $wover_time=10;

    //托管开启,开启全局托管模式，自动打牌摸牌打牌，true：开启， false：关闭
    public  $autoplay  =false;
    /*
     * 构造函数
     * @param $cards  初始化麻将数据
     */
    public function __construct($cards = array()) {
        if (empty($cards)) {
            $index = 0;
            //筒 0 ~ 8表示筒子
            for($i = 0; $i < 9; ++$i) {
                for($c = 0; $c < 4; ++$c){
                    $cards[$index] = $i;
                    $index++;
                }
            }
            //条 10 ~ 18表示条子
            for($i = 10; $i < 19; ++$i) {
                for($c = 0; $c < 4; ++$c){
                    $cards[$index] = $i;
                    $index++;
                }
            }
            //万 20 ~ 28表示万
            for($i = 20; $i < 29; ++$i) {
                for($c = 0; $c < 4; ++$c){
                    $cards[$index] = $i;
                    $index++;
                }
            }
            //字牌 30 ~ 37表示字牌
            for($i = 30; $i < 37; ++$i) {
                for($c = 0; $c < 4; ++$c){
                    $cards[$index] = $i;
                    $index++;
                }
            }

            //字牌 40 ~ 48表示花牌
            for($i = 40; $i < 48; ++$i) {
                $cards[$index] = $i;
                $index++;
            }
            $this->cards['pai'] = $cards;
            //将144张牌的顺序打乱，相当于洗牌
            shuffle($this->cards['pai']);
        } else {
            $this->cards['pai'] = $cards;
        }
    }
    /**
     * 发牌
     * @param  [int] $playerCount [选手人数]
     * @return [bool] false |  [array]    [牌的数组]
     */
    public function create($playerCount) {
        if($playerCount<0 or $playerCount>4){
            return false;
        }
        $array = $this->cards['pai'];
        $allCount = $playerCount * 13;
        for ($i = 0; $i < $allCount; $i++) {
            $paiInfo[floor($i / 13)][] = $array[$i];
        }
        //庄家的编号任何时候都为0
        $paiInfo[0][]=$array[$allCount];
        //余牌(包括余下的花牌)
        $yupai=array_slice($array,$allCount+1);
        foreach ($yupai as $key=>$value) {
            if ($value>=40 && $value<48) {
                $this->cards['yuhuapai'][]=$value;
                unset($yupai[$key]);
            }
        }
        $this->cards['yupai']=array_values( $yupai);
        $this->num++;
        //再次打乱剩余的牌
        shuffle($this->cards['yupai']);
        $this->buhua($paiInfo);
        unset($this->cards['pai']);

    }
    /**
     * 获取所有的的牌
     */
    public function getallcards(){
        $c['playcards']=$this->playcards;
        $c['cards']=$this->cards;
        $c['turn']=$this->turn;
        $c['dcards']=$this->dcards;
        $c['zhuang']=$this->zhuang;
        return $c;
    }
    /**
     * 设置所有的的牌
     */
    public function setallcards($c){
        $this->playcards=$c['playcards'];
        $this->cards=$c['cards'];
        $this->turn=$c['turn'];
        $this->dcards=$c['dcards'];
        $this->zhuang=$c['zhuang'];
    }
    /**
     * 获取玩家的牌，默认获取所有玩家的牌
     */
    public function getplaycards($index=-1){
        return $index>-1?$this->playcards[$index]:$this->playcards;
    }
    /**
     * 摸牌（1）打牌（2），吃牌（3），碰牌（4），点杠（5），暗杠（6），弯杠（7），过（8），胡牌（9），荒局（10）
     * @param   $turn   当前转向
     * 摸牌~~~  服务器推送
     * 打牌~~~ $data['index']=0     打牌者id        $data['cur_dpai']=int;  打的牌
     * 吃牌~~~  $data['index']=0    吃牌者id
     * 碰牌~~~ $data['index']=0     碰牌者id
     * 点杠~~~ $data['index']=0     点杠者id
     * 暗杠~~~ $data['index']=0     暗杠者id
     * 弯杠~~~ $data['index']=0     弯杠者id
     * 过       $data['index']=0     过牌者id       因为优先推送给的是杠或者碰，所以如果过的是杠或者碰，就找是否可以吃的，如果过的是吃的，那就正常叫牌
     *          $data['kind']=1     1：吃， 2碰牌，3点杠，4暗杠，5弯杠,6吃碰，7吃杠
     *胡牌~~~  $data['index']=0   胡牌者id
     *          $data['wintype']=2  2.别人打牌自己胡  3.自己摸牌自己胡 4.杠上开花（点杠） 5.杠上开花（暗杠）  6.杠上开花（弯杠）
     *          $data['type']=1   1:自摸,2:放冲
     * @return [bool] false |  true
     */
    public function actionpai($action,$data){
        $return_data=['msg'=>'','type'=>0,'data'=>[],'alldata'=>[],'turn'=>0,'action'=>[],'hu'=>[]];
        //最后的出牌id，最后的出牌数据
        if($this->dcards){
            $dcards=end($this->dcards);
            $dindex=array_keys($dcards)[0];
            $dpai=$dcards[$dindex];
            $data['dindex']=$dindex;  //打牌者座位id
            $data['dpai']=$dpai;      //打牌者打的牌
        }
        if($action==1){   //摸牌
            //判断是否是该用户去摸牌
            if($data['indexdata'] && $data['index']>=0){
                //判断前端传的牌和自己的牌是否一样
                $result=array_diff($data['indexdata'],$this->playcards[$data['index']]['pai']);
                if($result){
                    $return_data=['msg'=>'数据错误，拒绝','type'=>0];
                    return  $return_data;
                }
                //进行摸牌
                $pai=$this->mopai($data['index'],$data['mopai_asc']);
                if($pai===false) {
                    $return_data=['msg'=>'摸牌失败','type'=>0];
                }else {

                    //摸牌不需要转向指针
                    //$this->turn++;
                    //if($this->turn>3)  $this->turn=0;
                    //sort($this->playcards[$data['index']]['pai']);
                    array_values($this->playcards[$data['index']]['pai']);

                    //判断是否自己可以暗杠
                    $angang=0;
                    $allcount=array_count_values($this->playcards[$data['index']]['pai']);
                    foreach($allcount as $k=>$v){
                        if($v>=4 && $k==$pai)  $angang=1;
                    }
                    //判断是否自己可以弯杠
                    $wgang=0;
                    if(@$this->playcards[$data['index']]['pengpai']){
                        foreach($this->playcards[$data['index']]['pengpai'] as $k=>$v){
                            foreach($v as $ks=>$vs){
                                $allcount=array_count_values($vs);
                                foreach($allcount as $kd=>$vd){
                                    if($vd>=3 && $kd==$pai)  $wgang=1;
                                }
                            }
                        }
                    }

                    //判断是否可以胡牌
                    $is_hu=$this->checkCanHuPai($this->playcards[$data['index']]['pai']);
                    $return_data=['msg'=>'摸牌成功','type'=>1,'data'=>$pai,'alldata'=>$this->playcards,'turn'=>$this->turn,'index'=>$data['index'],'action'=>''];
                    $return_data['action']=array();
                    if($angang )           $return_data['action'][]='angang';
                    if($wgang)             $return_data['action'][]='wgang';
                    if($is_hu!=false)     $return_data['action'][]='hu';
                }
            }else {
                $return_data=['msg'=>'数据错误，拒绝','type'=>0];
            }
        }
        elseif($action==2) {  //打牌
            $data['dpai']=$data['cur_dpai'];
            if ($data['index'] >= 0 && $data['dpai'] >= 0 && $this->playcards[$data['index']]) {
                //查询打出的牌是否存在于自己的牌中
                $fs = array_search($data['dpai'], $this->playcards[$data['index']]['pai']);
                if ($fs === false) {
                    $return_data = ['msg' => '打出的牌不存在于自己牌中，拒绝', 'type' => 0];
                    return $return_data;
                }
                //查询打出的牌是否存在于自己的禁止打牌中
                $fs = array_search($data['dpai'], $this->playcards[$data['index']]['dispai']);
                if ($fs!=false) {
                    $return_data = ['msg' => '因为你吃了上家的牌，当前牌不允许打出，拒绝', 'type' => 0];
                    return $return_data;
                }

                //自己牌堆添加一张牌
                array_push( $this->playcards[$data['index']]['dapai'],$this->playcards[$data['index']]['pai'][$fs]);
                //牌流水数据添加一张
                array_push($this->dcards,array($data['index']=>$this->playcards[$data['index']]['pai'][$fs]));
                //删除这张牌
                unset($this->playcards[$data['index']]['pai'][$fs]);
                //删除被当前被禁止打出的牌，下次可以正常打出
                if($this->playcards[$data['index']]['dispai']) $this->playcards[$data['index']]['dispai']=[];
                //更新转向指针，更新牌面
                $this->turn++;
                if ($this->turn > 3) $this->turn = 0;
                sort($this->playcards[$data['index']]['pai']);
                array_values($this->playcards[$data['index']]['pai']);

                //判断其他玩家是否可以碰
                $peng = -1;
                // $allcount=array_count_values($this->playcards[$data['index']]['pai']);
                foreach ($this->playcards as $k => $v) {
                    $st = $this->checkCanPeng($v['pai'], $data['dpai']);
                    if ($st) $peng = $k;
                }
                //判断其他玩家是否可以杠
                $gang = -1;
                // $allcount=array_count_values($this->playcards[$data['index']]['pai']);
                foreach ($this->playcards as $k => $v) {
                    $sts = $this->checkCanDianGang($v['pai'], $data['dpai']);
                    if ($sts) $gang = $k;
                }
                //判断下家是否可以吃
                $chi = -1;
                $xiajia = $data['index'] + 1;
                if ($xiajia > 3) $xiajia = 0;
                $stu = $this->checkCanChi($this->playcards[$xiajia]['pai'], $data['dpai']);
                $chi = $stu != false ? $xiajia : -1;

                //判断其他玩家拿到这张牌是否可以胡，然后推送给其他玩家（同一家牌上一家打出不碰胡，则下家打出不可碰胡【前提：对胡 AABB  或者 ABCD】）
                $hu=[];
                foreach ($this->playcards as $k => $v) {
                    if($k!=$data['index']){
                        array_push($v['pai'],$data['dpai']);
                        $is_hu=$this->checkCanHuPai($v['pai'],$data['dpai'],$k);
                        if ($is_hu!==false) $hu[]=$k;
                    }
                }
                $return_data = ['msg' => '打牌成功', 'type' => 1, 'data' => $data['dpai'],'index'=>$data['index'], 'turn' => $this->turn];
                $return_data['action']['chi']=-1;
                $return_data['action']['peng']=-1;
                $return_data['action']['gang']=-1;
                $return_data['hu']=[];
                if ($peng < 0 && $gang < 0 && $chi >= 0) {
                    $return_data['action']['chi']= $chi;
                } else {
                    if ($peng >= 0) $return_data['action']['peng'] = $peng;
                    if ($gang >= 0) $return_data['action']['gang']=  $gang;
                }
                if($chi==$peng ){
                    $return_data['action']['chipeng']= $chi;
                }
                if($chi==$gang ){
                    $return_data['action']['chigang']=$chi;
                }
                if (count($hu)>0) {
                    $return_data['hu'] = $hu;
                }
                //判断如果没有人可以吃碰杠胡，则可以进行摸牌操作，然后推送给摸牌的人操作
                if($peng>=0 || $gang>=0 || $chi>=0 || count($hu)>0){
                    $return_data['mp']=0;
                    $return_data['mopaiinfo']=[];
                }else{
                    $return_data['mp']=1;
                    //摸牌玩家ID,进行摸牌
                    $mindex=$this->turn;
                    $mopai_asc=1;
                    $pai=$this->mopai($mindex,$mopai_asc);
                    if($pai===false) {
                        $return_data['mopaiinfo']=['msg'=>'牌都没有了,扎心了,老铁','type'=>0];
                    }else {
                        array_values($this->playcards[$mindex]['pai']);
                        //判断是否自己可以暗杠
                        $angang=0;
                        $allcount=array_count_values($this->playcards[$mindex]['pai']);
                        foreach($allcount as $k=>$v){
                            if($v>=4 && $k==$pai)  $angang=1;
                        }
                        //判断是否自己可以弯杠
                        $wgang=0;
                        if(@$this->playcards[$mindex]['pengpai']){
                            foreach($this->playcards[$mindex]['pengpai'] as $k=>$v){
                                foreach($v as $ks=>$vs){
                                    $allcount=array_count_values($vs);
                                    foreach($allcount as $kd=>$vd){
                                        if($vd>=3 && $kd==$pai)  $wgang=1;
                                    }
                                }
                            }
                        }
                        //判断是否可以胡牌
                        $is_hu=$this->checkCanHuPai($this->playcards[$mindex]['pai']);
                        $return_data['mopaiinfo']=['msg'=>'摸牌成功','type'=>1,'data'=>$pai,'index'=>$mindex,'account_id'=>$this->zhuang[$mindex],'action'=>''];
                        $return_data['mopaiinfo']['action']=array();
                        if($angang )           $return_data['mopaiinfo']['action'][]='angang';
                        if($wgang)             $return_data['mopaiinfo']['action'][]='wgang';
                        if($is_hu!=false)     $return_data['mopaiinfo']['action'][]='hu';
                    }

                }

            }
            else{
                return  $return_data=['msg'=>'数据错误，拒绝','type'=>0];
            }
        }elseif($action==3){   //吃牌，前端不能传数据过来，只能靠自己去找了，悲剧...
            if($data['dindex']>=0 && $data['dpai']>=0 && $data['index']>=0 ){
                $stu = $this->checkCanChi($this->playcards[$data['dindex']]['pai'], $data['dpai']);
                if($stu==false){
                    $return_data=['msg'=>'无法执行吃牌要求，拒绝','type'=>0];
                    return  $return_data;
                }
                //吃牌者要吃的牌
                $data['ppai']=$stu;

                //查询吃的牌是否存在于自己的牌中
                $fs1=array_diff($data['ppai'],$this->playcards[$data['index']]['pai']);
                if($fs1){
                    $return_data=['msg'=>'要吃的牌不存在于自己牌中，拒绝','type'=>0];
                    return  $return_data;
                }

                //是否上下级关系
                $guanxi=$this->isshangjia($data['dindex'],$data['index']);
                if($guanxi==false){
                    $return_data=['msg'=>'不是您的上家，无法吃牌，拒绝','type'=>0];
                    return  $return_data;
                }
                //是否组成一句话
                $yiuhua=array_merge($data['ppai'],$data['dpai']);
                sort($yiuhua);
                if(@$yiuhua[0]+1==@$yiuhua[1] &&  @$yiuhua[1]+1==@$yiuhua[2]){
                    //自己吃牌堆添加要吃的牌
                    $this->playcards[$data['index']]['chipai'][$data['dindex']][]= $yiuhua;

                    //删除这两张牌
                    $this->playcards[$data['index']]['pai']=array_diff($this->playcards[$data['index']],$data['ppai']);

                    //判断是否吃碰杠两次以上，吃碰杠同一家两次以前不允许吃什么打什么。（例如：手上有3456条，吃了6条，不仅6条不能打，3条也不能打）
                    $getdis=array('type'=>1,'dindex'=>$data['dindex'],'index'=>$data['index'],'dpai'=>$data['dpai'],'allpai'=>$data['ppai']);
                    $disable_pai=$this->GetDisablePai($getdis);
                    if($disable_pai!=false){
                        $this->playcards[$data['index']]['dispai']=$disable_pai;
                    }

                    //更新转向指针，更新牌面
                    $this->turn=$data['index'];
                    //if($this->turn>3)  $this->turn=0;
                    sort($this->playcards[$data['index']]['pai']);
                    array_values($this->playcards[$data['index']]['pai']);
                    //判断是否翻杠牌
                    $this->IsUpdateOpenGang($data['index']);
                    $return_data=['msg'=>'吃牌成功','type'=>1,'data'=>$yiuhua,'index'=>$data['index']];


                }else{
                    $return_data=['msg'=>'不能组成一句话，无法吃牌，拒绝','type'=>0];
                    return  $return_data;
                }
            }else{
                $return_data=['msg'=>'数据错误，拒绝','type'=>0];
            }
        }elseif($action==4){  //碰牌，前端不能传数据过来，只能靠自己去找了，悲剧...
            if($data['dindex']>=0 && $data['dpai']>=0 && $data['index']>=0){
                $data['ppai']=[$data['dpai'],$data['dpai']];
                //查询碰的牌是否存在于自己的牌中
                $fs1=array_diff($data['ppai'],$this->playcards[$data['index']]['pai']);
                if($fs1){
                    $return_data=['msg'=>'碰的牌不存在于自己牌中，拒绝','type'=>0];
                    return  $return_data;
                }
                //是否一样
                if($data['ppai'][0]!=$data['ppai'][1]){
                    $return_data=['msg'=>'碰牌不一样，拒绝','type'=>0];
                    return  $return_data;
                }
                //是否组成一刻字
                $yiuhua=array_merge($data['ppai'],[$data['dpai']]);
                if(@$yiuhua[0]==@$yiuhua[1] &&  @$yiuhua[1]==@$yiuhua[2]){
                    //自己吃牌堆添加要碰的牌
                    $this->playcards[$data['index']]['pengpai'][$data['dindex']][]= $yiuhua;
                    //删除这两张牌
                    $this->playcards[$data['index']]['pai']=array_diff($this->playcards[$data['index']]['pai'],$data['ppai']);
                    //判断是否吃碰杠两次以上，吃碰杠同一家两次以前不允许吃什么打什么。（例如：手上有3456条，吃了6条，不仅6条不能打，3条也不能打）
                    $getdis=array('type'=>1,'dindex'=>$data['dindex'],'index'=>$data['index'],'dpai'=>$data['dpai'],'allpai'=>$data['ppai']);
                    $disable_pai=$this->GetDisablePai($getdis);
                    if($disable_pai!=false){
                        $this->playcards[$data['index']]['dispai']=$disable_pai;
                    }
                    //更新转向指针，更新牌面
                    $this->turn=$data['index'];
                    //if($this->turn>3)  $this->turn=0;
                    sort($this->playcards[$data['index']]['pai']);
                    array_values($this->playcards[$data['index']]['pai']);
                    //判断是否翻杠牌
                    $this->IsUpdateOpenGang($data['index']);
                    $return_data=['msg'=>'碰牌成功','type'=>1,'data'=>$yiuhua,'index'=>$data['index'],'alldata'=>$this->playcards,'turn'=>$this->turn];

                }else{
                    $return_data=['msg'=>'不能组成一刻字，无法碰牌，拒绝','type'=>0];
                    return  $return_data;
                }
            }else{
                $return_data=['msg'=>'数据错误，拒绝','type'=>0];
            }
        }elseif($action==5){  //点杠
            if($data['dindex']>=0 && $data['dpai']>=0 && $data['index']>=0){
                $data['ppai']=[$data['dpai'],$data['dpai'],$data['dpai']];
                //查询杠的牌是否存在于自己的牌中
                $fs1=array_diff($data['ppai'],$this->playcards[$data['index']]['pai']);
                if($fs1){
                    $return_data=['msg'=>'要杠的牌不存在于自己牌中，拒绝','type'=>0];
                    return  $return_data;
                }
                //是否组成一杠
                $yiuhua=array_merge($data['ppai'],$data['dpai']);
                if(@$yiuhua[0]==@$yiuhua[1] &&  @$yiuhua[1]==@$yiuhua[2]  &&  @$yiuhua[2]==@$yiuhua[3]){
                    //自己点杠牌堆添加要点杠的牌
                    $this->playcards[$data['index']]['mgangpai'][$data['dindex']][]= $yiuhua;
                    //删除这三张牌
                    $this->playcards[$data['index']]['pai']=array_diff($this->playcards[$data['index']]['pai'],$data['ppai']);
                    //更新转向指针，更新牌面
                    $this->turn=$data['index'];
                    //if($this->turn>3)  $this->turn=0;
                    //往剩下牌堆的牌添加2张
                    $this->UpdateGangPai();
                    sort($this->playcards[$data['index']]['pai']);
                    array_values($this->playcards[$data['index']]['pai']);
                    //判断是否翻杠牌
                    $this->IsUpdateOpenGang($data['index']);
                    $return_data=['msg'=>'杠牌成功','type'=>1,'data'=>$yiuhua,'index'=>$data['index']];
                    //杠牌还可以再摸一张
                    $return_data['mp']=1;
                    //摸牌玩家ID,进行摸牌
                    $mindex=$this->turn;
                    $mopai_asc=1;
                    $pai=$this->mopai($mindex,$mopai_asc);
                    if($pai===false) {
                        $return_data['mopaiinfo']=['msg'=>'牌都没有了,扎心了,老铁','type'=>0];
                    }else {
                        array_values($this->playcards[$mindex]['pai']);
                        //判断是否自己可以暗杠
                        $angang=0;
                        $allcount=array_count_values($this->playcards[$mindex]['pai']);
                        foreach($allcount as $k=>$v){
                            if($v>=4 && $k==$pai)  $angang=1;
                        }
                        //判断是否自己可以弯杠
                        $wgang=0;
                        if(@$this->playcards[$mindex]['pengpai']){
                            foreach($this->playcards[$mindex]['pengpai'] as $k=>$v){
                                foreach($v as $ks=>$vs){
                                    $allcount=array_count_values($vs);
                                    foreach($allcount as $kd=>$vd){
                                        if($vd>=3 && $kd==$pai)  $wgang=1;
                                    }
                                }
                            }
                        }
                        //判断是否可以胡牌
                        $is_hu=$this->checkCanHuPai($this->playcards[$mindex]['pai']);
                        $return_data['mopaiinfo']=['msg'=>'摸牌成功','type'=>1,'data'=>$pai,'index'=>$mindex,'account_id'=>$this->zhuang[$mindex],'action'=>''];
                        $return_data['mopaiinfo']['action']=array();
                        if($angang )           $return_data['mopaiinfo']['action'][]='angang';
                        if($wgang)             $return_data['mopaiinfo']['action'][]='wgang';
                        if($is_hu!=false)     $return_data['mopaiinfo']['action'][]='hu';
                    }


                }else{
                    $return_data=['msg'=>'不能组成一杠，无法碰牌，拒绝','type'=>0];
                    return  $return_data;
                }
            }else{
                $return_data=['msg'=>'数据错误，拒绝','type'=>0];
            }
        }elseif($action==6){  //暗杠，前端不能传数据过来，只能靠自己去找了，悲剧...
            $dpai=end($this->playcards[$data['index']]['mopai']);
            $data['ppai']=[$dpai,$dpai,$dpai,$dpai];
            if( $data['index']>=0 && $data['ppai']){
                ///查询暗杠的牌是否存在于自己的牌中
                $fs1=array_diff($data['ppai'],$this->playcards[$data['index']]['pai']);
                if($fs1){
                    $return_data=['msg'=>'杠的牌不存在于自己牌中，拒绝','type'=>0];
                    return  $return_data;
                }
                //是否组成一杠
                $yiuhua=$data['ppai'];
                //自己暗杠牌堆添加要点杠的牌
                $this->playcards[$data['index']]['agangpai'][$data['index']][]= $yiuhua;
                //删除这四张牌
                $this->playcards[$data['index']]['pai']=array_diff($this->playcards[$data['index']]['pai'],$data['ppai']);
                //更新转向指针，更新牌面
                $this->turn=$data['index'];
                if($this->turn>3)  $this->turn=0;
                //往剩下牌堆的牌添加2张
                $this->UpdateGangPai();
                sort($this->playcards[$data['index']]['pai']);
                array_values($this->playcards[$data['index']]['pai']);
                //判断是否翻杠牌
                $this->IsUpdateOpenGang($data['index']);
                $return_data=['msg'=>'杠牌成功','type'=>1,'data'=>$yiuhua,'index'=>$data['index'],'turn'=>$this->turn];
                //杠牌成功还要摸一张
                $return_data['mp']=1;
                //摸牌玩家ID,进行摸牌
                $mindex=$this->turn;
                $mopai_asc=1;
                $pai=$this->mopai($mindex,$mopai_asc);
                if($pai===false) {
                    $return_data['mopaiinfo']=['msg'=>'牌都没有了,扎心了,老铁','type'=>0];
                }else {
                    array_values($this->playcards[$mindex]['pai']);
                    //判断是否自己可以暗杠
                    $angang=0;
                    $allcount=array_count_values($this->playcards[$mindex]['pai']);
                    foreach($allcount as $k=>$v){
                        if($v>=4 && $k==$pai)  $angang=1;
                    }
                    //判断是否自己可以弯杠
                    $wgang=0;
                    if(@$this->playcards[$mindex]['pengpai']){
                        foreach($this->playcards[$mindex]['pengpai'] as $k=>$v){
                            foreach($v as $ks=>$vs){
                                $allcount=array_count_values($vs);
                                foreach($allcount as $kd=>$vd){
                                    if($vd>=3 && $kd==$pai)  $wgang=1;
                                }
                            }
                        }
                    }
                    //判断是否可以胡牌
                    $is_hu=$this->checkCanHuPai($this->playcards[$mindex]['pai']);
                    $return_data['mopaiinfo']=['msg'=>'摸牌成功','type'=>1,'data'=>$pai,'index'=>$mindex,'account_id'=>$this->zhuang[$mindex],'action'=>''];
                    $return_data['mopaiinfo']['action']=array();
                    if($angang )           $return_data['mopaiinfo']['action'][]='angang';
                    if($wgang)             $return_data['mopaiinfo']['action'][]='wgang';
                    if($is_hu!=false)     $return_data['mopaiinfo']['action'][]='hu';
                }



            }else{
                $return_data=['msg'=>'数据错误，拒绝','type'=>0];
            }
        }elseif($action==7){  //弯杠，前端不能传数据过来，只能靠自己去找了，悲剧...
            if( $data['index']>=0 && $data['dpai']){
                //查询杠的牌是否存在于自己的碰牌中
                $is_peng=-1;
                if($this->playcards[$data['index']]['pengpai']){
                    foreach($this->playcards[$data['index']]['pengpai'] as $k=>$v) {
                        $nums=array_count_values($v);
                        if(@$nums[$data['dpai']]>=3) $is_peng=$k;
                    }
                }
                //判断是否自己可以弯杠
                $is_peng=-1;
                $row=[];
                if(@$this->playcards[$data['index']]['pengpai']){
                    foreach($this->playcards[$data['index']]['pengpai'] as $k=>$v){
                        foreach($v as $ks=>$vs){
                            $allcount=array_count_values($vs);
                            foreach($allcount as $kd=>$vd){
                                if(@$allcount[$data['dpai']]>=3) {
                                    $row=$vs;
                                    $is_peng=$k;
                                }
                            }
                        }
                    }
                }

                if($is_peng<0){
                    $return_data=['msg'=>'要杠的牌不存在于自己牌中，拒绝','type'=>0];
                    return  $return_data;
                }

                //是否组成一杠
                $yiuhua=array_merge($data['ppai'],$row);
                if(@$yiuhua[0]==@$yiuhua[1] &&  @$yiuhua[1]==@$yiuhua[2]  &&  @$yiuhua[2]==@$yiuhua[3]){
                    //自己弯杠牌堆添加要弯杠的牌
                    $this->playcards[$data['index']]['wgangpai'][$data['dindex']][]= $data['ppai'];

                    //更新转向指针，更新牌面
                    $this->turn=$data['index'];
                    sort($this->playcards[$data['index']]['pai']);
                    array_values($this->playcards[$data['index']]['pai']);
                    $return_data=['msg'=>'杠牌成功','type'=>1,'data'=>$yiuhua,'index'=>$data['index']];

                    $return_data['mp']=1;
                    //摸牌玩家ID,进行摸牌
                    $mindex=$this->turn;
                    $mopai_asc=1;
                    $pai=$this->mopai($mindex,$mopai_asc);
                    if($pai===false) {
                        $return_data['mopaiinfo']=['msg'=>'牌都没有了,扎心了,老铁','type'=>0];
                    }else {
                        array_values($this->playcards[$mindex]['pai']);
                        //判断是否自己可以暗杠
                        $angang = 0;
                        $allcount = array_count_values($this->playcards[$mindex]['pai']);
                        foreach ($allcount as $k => $v) {
                            if ($v >= 4 && $k == $pai) $angang = 1;
                        }
                        //判断是否自己可以弯杠
                        $wgang = 0;
                        if (@$this->playcards[$mindex]['pengpai']) {
                            foreach ($this->playcards[$mindex]['pengpai'] as $k => $v) {
                                foreach ($v as $ks => $vs) {
                                    $allcount = array_count_values($vs);
                                    foreach ($allcount as $kd => $vd) {
                                        if ($vd >= 3 && $kd == $pai) $wgang = 1;
                                    }
                                }
                            }
                        }
                        //判断是否可以胡牌
                        $is_hu = $this->checkCanHuPai($this->playcards[$mindex]['pai']);
                        $return_data['mopaiinfo']=['msg'=>'摸牌成功','type'=>1,'data'=>$pai,'index'=>$mindex,'account_id'=>$this->zhuang[$mindex],'action'=>''];
                        $return_data['mopaiinfo']['action'] = array();
                        if ($angang) $return_data['mopaiinfo']['action'][] = 'angang';
                        if ($wgang) $return_data['mopaiinfo']['action'][] = 'wgang';
                        if ($is_hu != false) $return_data['mopaiinfo']['action'][] = 'hu';
                    }
                }else{
                    $return_data=['msg'=>'不能组成一杠，无法杠牌，拒绝','type'=>0];
                    return  $return_data;
                }
            }else{
                $return_data=['msg'=>'数据错误，拒绝','type'=>0];
            }
        }elseif($action==8) {  //过，前端不能传数据过来，只能靠自己去找了，悲剧...
            if ($data['index'] >= 0 && $data['kind']) {
                if($data['kind']==1 || $data['kind']==4 || $data['kind']==5){   //吃牌过
                    $return_data = ['msg' => '过过过', 'type' => 1, 'data' => $data['dpai'],'index' => $data['dindex'], 'xiajia' => -1,];
                    $mopai=1;
                }elseif($data['kind']==2 ||  $data['kind']==3){   //碰牌过 vs  点杠过
                    //判断下家是否可以吃
                    $chi = -1;
                    $xiajia = $data['index'] + 1;
                    if ($xiajia > 3) $xiajia = 0;
                    $stu = $this->checkCanChi($this->playcards[$xiajia]['pai'], $data['dpai']);
                    $chi = $stu != false ? $xiajia : -1;
                    if($chi>=0){
                        $return_data = ['msg' => '可以吃牌', 'type' => 1, 'data' => $data['dpai'],'index' => $data['dindex'], 'xiajia' => $xiajia];
                        $return_data['action'] = array('chi'=>$chi);
                        $mopai=0;
                    }else{
                        $return_data = ['msg' => '过过过', 'type' => 1, 'data' => $data['dpai'],'index' => $data['dindex'], 'xiajia' => -1];
                        $mopai=1;
                    }
                }elseif($data['kind']==4 || $data['kind']==5){   //暗杠过 vs  弯杠过
                    $return_data = ['msg' => '过过过', 'type' => 1, 'data' => $data['dpai'],'index' => $data['dindex'], 'xiajia' => -1];
                    $mopai=0;
                }elseif($data['kind']==6 || $data['kind']==7){   //吃碰 vs  吃杠
                    $return_data = ['msg' => '过过过', 'type' => 1, 'data' => $data['dpai'],'index' => $data['dindex'], 'xiajia' => -1];
                    $mopai=1;
                }
                //提示下一位可以摸牌
                if($mopai>0) {
                    $return_data['mp'] = 1;
                    //摸牌玩家ID,进行摸牌
                    $mindex = $this->turn;
                    $mopai_asc = 1;
                    $pai = $this->mopai($mindex, $mopai_asc);
                    if ($pai === false) {
                        $return_data['mopaiinfo'] = ['msg' => '牌都没有了,扎心了，老铁', 'type' => 0];
                    } else {
                        array_values($this->playcards[$mindex]['pai']);
                        //判断是否自己可以暗杠
                        $angang = 0;
                        $allcount = array_count_values($this->playcards[$mindex]['pai']);
                        foreach ($allcount as $k => $v) {
                            if ($v >= 4 && $k == $pai) $angang = 1;
                        }
                        //判断是否自己可以弯杠
                        $wgang = 0;
                        if (@$this->playcards[$mindex]['pengpai']) {
                            foreach ($this->playcards[$mindex]['pengpai'] as $k => $v) {
                                foreach ($v as $ks => $vs) {
                                    $allcount = array_count_values($vs);
                                    foreach ($allcount as $kd => $vd) {
                                        if ($vd >= 3 && $kd == $pai) $wgang = 1;
                                    }
                                }
                            }
                        }
                        //判断是否可以胡牌
                        $is_hu = $this->checkCanHuPai($this->playcards[$mindex]['pai']);
                        $return_data['mopaiinfo']=['msg'=>'摸牌成功','type'=>1,'data'=>$pai,'index'=>$mindex,'account_id'=>$this->zhuang[$mindex],'action'=>''];
                        $return_data['mopaiinfo']['action'] = array();
                        if ($angang) $return_data['mopaiinfo']['action'][] = 'angang';
                        if ($wgang) $return_data['mopaiinfo']['action'][] = 'wgang';
                        if ($is_hu != false) $return_data['mopaiinfo']['action'][] = 'hu';
                    }
                }
            }
        }elseif($action==9) {
            //胡牌，检测胡牌，并且统计此次胡牌数据结果，返回给前端
            //胡牌有两种情况，要么自摸要么别人打牌自己胡，先从自己的摸牌流水里面找，找打了就说明是自己自摸

            if($data['index']>=0 && in_array($data['wintype'],array(2,3,4,5,6))){
                if($data['wintype']==2){      //别人打牌自己胡，找到最后一张打出来的牌，然后检测是否胡牌
                    $hu=[];
                    foreach ($this->playcards as $k => $v) {
                        if($k!=$data['index']){
                            array_push($v['pai'],$data['dpai']);
                            $is_hu=$this->checkCanHuPai($v['pai'],$data['dpai'],$k);
                            if ($is_hu!==false) $hu[$k]=$is_hu;
                        }
                    }
                    //找出所有的胡牌者，有可能涉及到一炮双响，所以必须循环一次，找出所有的赢者
                    if($hu){
                        if(array_key_exists($data['index'],$hu)){
                            // 检测胡牌规则，得到正确的番数


                        }else{
                            $return_data=['msg'=>'当前牌无法胡牌，拒绝','type'=>0];
                        }
                    }else{
                        $return_data=['msg'=>'当前牌无法胡牌，拒绝','type'=>0];
                    }



                    $is_hu=$this->checkCanHuPai($this->playcards[$data['index']]['pai'],$data['dpai'],$data['index']);
                    if($is_hu==false){
                        $return_data=['msg'=>'当前牌无法胡牌，拒绝','type'=>0];
                    }else{
                        //  检测胡牌规则，得到正确的番数



                    }
                }elseif($data['wintype']==3 || $data['wintype']==4 || $data['wintype']==5 || $data['wintype']==6 ){    //自己摸牌自己胡牌，然后检测是否胡牌
                    $is_hu=$this->checkCanHuPai($this->playcards[$data['index']]['pai']);
                    if($is_hu==false){
                        $return_data=['msg'=>'当前牌无法胡牌，拒绝','type'=>0];
                    }else{
                        //  检测胡牌规则，得到正确的番数
                    }
                }





                $return_data = ['msg' => '胡牌了', 'type' => 1, 'data' => $data['dpai'],'index' => $data['dindex'], 'alldata' => $this->playcards, 'turn' => $this->turn];
                $return_data['score']=array(0=>150,1=>-50,2=>-50,3=>-50);

            }else{
                $return_data=['msg'=>'数据错误，拒绝','type'=>0];
            }

            return $return_data;
        }elseif($action==10) {
            //检测余牌是否还有，没有的话直接荒局，返回当前局排行榜
            $total=count($this->cards['yupai']);
            if($total>=0){
                $return_data=['msg'=>'当前还有剩余牌，拒绝','type'=>0];
            }else{
                $return_data=['msg'=>'流局成功','type'=>1];
            }
        }
        $return_data['yupai']=count($this->cards['yupai']);
        $return_data['autoplay']=$this->autoplay;
        $return_data['zhuang']=$this->zhuang;
        return $return_data;
    }
    //丢色子
    public function rengshaizi(){
        $shaizi=array(rand(1,6),rand(1,6));
        if($shaizi[0]+$shaizi[1]==2 || $shaizi[0]+$shaizi[1]==6 || $shaizi[0]+$shaizi[1]==10){
            $zhuang=2;
        }elseif($shaizi[0]+$shaizi[1]==3 || $shaizi[0]+$shaizi[1]==7 || $shaizi[0]+$shaizi[1]==11){
            $zhuang=3;
        }elseif($shaizi[0]+$shaizi[1]==4 || $shaizi[0]+$shaizi[1]==8 || $shaizi[0]+$shaizi[1]==12){
            $zhuang=4;
        }else{
            $zhuang=1;
        }
        return ['shanzi'=>$shaizi,'zhuang'=>$zhuang];
    }
    //查询上家
    private function isshangjia($shangjia,$self){
        if($shangjia==0 &&  $self==1) return true;
        if($shangjia==1 &&  $self==2) return true;
        if($shangjia==2 &&  $self==3) return true;
        if($shangjia==3 &&  $self==0) return true;
        return false;
    }
    /**
     * 定义牌的类型
     * @param  [int] $id  [牌符]
     * @return [bool] false |  [array]    [牌的类型]
     */
    private function getMJType($id){
        if($id >= 0 && $id < 9){
            //筒
            return 0;
        }
        else if($id >= 10 && $id < 19){
            //条
            return 1;
        }
        else if($id >= 20 && $id < 29){
            //万
            return 2;
        }
        else if($id >= 30 && $id < 37){
            //字牌
            return 3;
        }
        else if($id >= 40 && $id < 48){
            //花牌
            return 4;
        }
    }
    /**
     * 补花牌
     * @param  [int] $playerCount [选手人数]
     * @return [bool] false |  [array]    [牌的数组]
     */
    private function buhua($paiInfo) {
        $index=0;
        foreach ($paiInfo as $key=>$value) {
            $pai=[];
            foreach ($value as $keys=>$values) {
                if ($values>=40 && $values<48){
                    ++$index;
                    $this->playcards[$key]['huapai'][]=$values;
                    $pai[]=$this->cards['yupai'][0];
                    unset($this->cards['yupai'][0]);
                    $this->cards['yupai']=array_values($this->cards['yupai']);
                }else{
                    $pai[]=$values;
                }
                sort($pai);
                $this->playcards[$key]['pai']=$pai;
            }
        }
        //判断花牌数量，得到正确的余牌,去除花，当8朵花不齐的情况下，桌面牌尾剩余14张牌时流局。当8朵花全齐的情况下，桌面牌尾剩余16张牌时流局
        $yupai=$this->cards['yupai'];
        if($index>=8){
            $this->cards['disyupai']=array_slice($yupai,0,16);
            $this->cards['yupai']=array_slice($this->cards['yupai'],16);
        }else{
            $this->cards['disyupai']=array_slice($yupai,0,14);
            $this->cards['yupai']=array_slice($this->cards['yupai'],14);
        }
        $this->cards['yupai']=array_values($this->cards['yupai']);
    }
    /**
     * 摸牌
     * @param  [int]  $index [选手],$xu=1  摸第一张或者  $xu=0最后一张
     * @return [bool] false | 摸到的牌
     */
    private function mopai($index,$xu=1) {
        if(count($this->cards['yupai'])>0){
            if($xu=1){
                $mopai=$this->playcards[$index]['pai'][]=$this->cards['yupai'][0];
                $this->cards['yupai']=array_slice($this->cards['yupai'],1);
            }else{
                $mopai=$this->playcards[$index]['pai'][]=end($this->cards['yupai']);
                $this->cards['yupai']=array_slice($this->cards['yupai'],0,-1);
            }
            array_push($this->playcards[$index]['mopai'],$mopai);
            $this->cards['yupai']=array_values($this->cards['yupai']);
            return $mopai;
        }
        return false;
    }
    /**
     * 吃碰杠同一家两次以前不允许吃什么打什么。（例如：手上有3456条，吃了6条，不仅6条不能打，3条也不能打）
     * $data['type']=1  int         1  吃牌，  2   碰牌   3 杠牌
     * $data['dindex']  int         被吃碰杠的人位置
     * $data['index']  int          吃碰杠的人位置
     * $data['dpai']    int         被吃碰杠的牌
     * $data['allpai']  array()     被吃碰杠的一组牌，不包含自己的牌
     * @param
     * $mgangpai=array(0=>array(array(1,1,1,1),array(2,2,2,2)),1=>array(array(3,3,3,3),array(4,4,4,4)));
     * $pengpai=array(0=>array(array(1,1,1,1),array(2,2,2,2)),1=>array(array(3,3,3,3),array(4,4,4,4)));
     * $chipai=array(0=>array(array(1,1,1,1),array(2,2,2,2)),1=>array(array(3,3,3,3),array(4,4,4,4)));
     */
    private function GetDisablePai($data) {
        $disable_pai=[];
        $mgangpai=$this->playcards[$data['dindex']]['mgangpai'];
        $pengpai=$this->playcards[$data['dindex']]['pengpai'];
        $chipai=$this->playcards[$data['dindex']]['chipai'];
        $num=0;
        if($mgangpai){
            foreach ($mgangpai as $k=>$v){
                if($k==$data['dindex']) $num+=count($v,0);
            }
        }
        if($pengpai){
            foreach ($pengpai as $k=>$v){
                if($k==$data['dindex']) $num+=count($v,0);
            }
        }
        if($chipai){
            foreach ($chipai as $k=>$v){
                if($k==$data['dindex']) $num+=count($v,0);
            }
        }
        if($num>2) return false;
        if($data['type']==1){
            $data['allpai']=array(3,4);
            sort($data['allpai']);
            if($data['allpai'][0]+1== $data['allpai'][1]){
                $disable_pai1=$data['allpai'][0]-1;
                $disable_pai2=$data['allpai'][1]+1;
                if(in_array($disable_pai1,$this->playcards[$data['index']]['pai'])){
                    $disable_pai[]=$disable_pai1;
                }
                if(in_array($disable_pai2,$this->playcards[$data['index']]['pai'])){
                    $disable_pai[]=$disable_pai2;
                }
            }else{
                $disable_pai[]=$data['dpai'];
            }
        }elseif($data['type']==2 || $data['type']==3) {
            if(in_array($data['dpai'],$this->playcards[$data['index']]['pai'])){
                $disable_pai[]=$data['dpai'];
            }
        }
        if($disable_pai) return $disable_pai;
        return false;
    }
    /**
     * 是否把暗杠打开一张
     * @param  [int]  $index
     * @return [bool] false | 摸到的牌
     */
    private function IsUpdateOpenGang($index) {
        $agangpai=$this->playcards[$index]['agangpai'];
        $mgangpai=$this->playcards[$index]['mgangpai'];
        $wgangpai=$this->playcards[$index]['wgangpai'];
        $pengpai=$this->playcards[$index]['pengpai'];
        $chipai=$this->playcards[$index]['chipai'];
        if($this->playcards[$index]['opengang']<1){
            if(($agangpai ||  $mgangpai  || $wgangpai) &&  $pengpai && $chipai){
                $this->playcards[$index]['opengang']=1;
            }
        }
    }
    /**
     * 杠牌调整余牌数量
     * @param   明杠或者暗杠
     */
    private function UpdateGangPai() {
        if(count($this->cards['yupai'])>=2){
            $del_pai=array_slice($this->cards['yupai'],0,2);
            array_push($this->cards['disyupai'],$del_pai);
            $this->cards['yupai']=array_slice($this->cards['yupai'],2);
        }else{
            array_push($this->cards['disyupai'],$this->cards['yupai']);
            $this->cards['yupai']=[];
        }
    }

    /**
     * 检查是否可以吃牌
     * @param  [array] $data  [吃牌的牌数组] ，[int]  $targetPai [要吃牌的牌]
     * @return [bool] false |  true
     */
    private function checkCanChi($data,$targetPai) {
        $type=$this->getMJType($targetPai);
        sort($data);
        switch ($type) {
            case 0://筒
                if(in_array($targetPai+1,$data) && in_array($targetPai+2,$data) && $targetPai+1<9  && $targetPai+2<9 )  return array($targetPai+1,$targetPai+2);
                if(in_array($targetPai-1,$data) && in_array($targetPai+1,$data) && $targetPai-1>=0  && $targetPai+1<9 ) return array($targetPai-1,$targetPai+1);
                if(in_array($targetPai-1,$data) && in_array($targetPai-2,$data) && $targetPai-1>=0  && $targetPai-2>9 ) return array($targetPai-2,$targetPai-1);
                return false;
                break;
            case 1://条
                if(in_array($targetPai+1,$data) && in_array($targetPai+2,$data) && $targetPai+1<19  && $targetPai+2<19 )   return array($targetPai+1,$targetPai+2);
                if(in_array($targetPai-1,$data) && in_array($targetPai+1,$data) && $targetPai-1>=10   && $targetPai+1<19 ) return array($targetPai-1,$targetPai+1);
                if(in_array($targetPai-1,$data) && in_array($targetPai-2,$data) && $targetPai-1>=10   && $targetPai-2>19 ) return array($targetPai-2,$targetPai-1);
                return false;
                break;
            case 2://万
                if(in_array($targetPai+1,$data) && in_array($targetPai+2,$data) && $targetPai+1<29  && $targetPai+2<29 )   return array($targetPai+1,$targetPai+2);
                if(in_array($targetPai-1,$data) && in_array($targetPai+1,$data) && $targetPai-1>=20   && $targetPai+1<29 ) return array($targetPai-1,$targetPai+1);
                if(in_array($targetPai-1,$data) && in_array($targetPai-2,$data) && $targetPai-1>=20   && $targetPai-2>29 ) return array($targetPai-2,$targetPai-1);
                return false;
                break;
            default:
                return false;
        }
    }
    /**
     * 检查是否可以碰
     * @param  [array] $data  [碰的牌数组] ，[int]  $targetPai [要碰的牌]
     * @return [bool] false |  true
     */
    private function checkCanPeng($data,$targetPai) {
        $data=array_count_values($data);
        $count = @$data[$targetPai];
        if($count != null && $count >= 2){
            return true;
        }
        return false;
    }
    /**
     * 检查是否可以杠，自己手上已经有3张牌，别人打出来，要点杠
     * @param  [array] $data  [杠的牌数组] ，[int]  $targetPai [要杠的牌]
     * @return [bool] false |  true
     */
    private function checkCanDianGang($data,$targetPai){
        if(count($this->cards['yupai'])<=0) return false;
        $data=array_count_values($data);
        $count = @$data[$targetPai];
        if($count != null && $count >= 3){
            return true;
        }
        return false;
    }
    /**
     * 检查是否可以暗杠，自己手上已经有3张牌，摸起来暗杠
     * @param  [array] $data  [暗杠的牌数组] ，[int]  $targetPai [要暗杠的牌]
     * @return [bool] false |  true
     */
    private function checkCanAnGang($data,$targetPai){
        if(count($this->cards['yupai'])<=0) return false;
        $data=array_count_values($data);
        $count = @$data[$targetPai];
        if($count != null && $count >= 4){
            return true;
        }
        return false;
    }
    /**
     * 检查是否可以弯杠(自己已经碰过，现在又摸起来1张一样，再杠)
     * @param  [array] $data  [弯杠的牌数组] ，[int]  $targetPai [要弯杠的牌]
     * @return [bool] false |  true
     */
    private function checkCanWanGang($data,$targetPai){
        if(count($this->cards['yupai'])<=0) return false;
        $data=array_count_values($data);
        $count = @$data[$targetPai];
        if($count != null && $count >= 3){
            return true;
        }
        return false;
    }
    /**
     * 检查是否可以胡牌[]
     * @param  [array] $pai  [胡的牌] ，$dapai  [打的牌] -1代表自摸  [int]  $index [胡牌的人]
     * @return [bool] false |  array
     */
    public function checkCanHuPai($pai,$dapai=-1,$index=-1){
        //检查是否是对对胡  前提是没有碰，也没有杠 ，即手上拥有13张牌，外加1张胡牌
        $par = [];
        $status=$this->isqidui($pai);
        //判断是否其他胡牌
        if(!$status){
            $status=$this->ispinghu($pai);
            //判断是不是碰胡，如果是的话，检测上家有没有打过，如果上家打过，也不能算胡牌
            if($dapai>=0 && $index>=0){
                $penghu=getwintype($status,$dapai); //是碰胡，检测上家是否打过
                if($penghu){
                    //检查当前打牌用户之前有没有打过此张牌
                    $isda=$this->is_updown_penghu($this->dcards,$dapai,$index);
                    if($isda==false) return false;
                }
            }
            return    $status;
        }else{
            return    $status;
        }
        return false;

    }
    /**
     * 判断是否碰胡,结合打牌流水检查
     * @param  [type]  $dcards [打牌流水牌]   $dapai  胡的那张  $index要胡的那个人
     * @return boolean  false | true
     */
    private function is_updown_penghu($dcards,$dapai,$index){
        $dcards=array_reverse($dcards,false);
        $num=0;
        foreach($dcards as $k=>$v){
            $data[]=$v;
            if(array_key_exists($index,$v)) break;
        }
        foreach ( $data as $item) {
            $item=array_values($item)[0];
            if($item==$dapai) $num++;
        }
        if($num>=2) return false;
        return true;
    }
    private function isqidui($pai){
        if(!$pai)return false;
        if(count($pai)!=14)return false;
        // 取出牌数字重复的次数
        $cards_nums=array_count_values($pai);
        $duizi=0;
        foreach ($cards_nums as $key => $val) {
            if($val===2){
                ++$duizi;
            }elseif($val==4){//四张相同就是两对
                $duizi+=2;
            }
        }
        if($duizi==7){
            sort($pai);
            return array_chunk($pai,2);
        }
        return false;
    }
    /**
     * 普通胡   1台
     * @param  [type]  $cards [牌]
     * @return boolean  false | array   失败返回false，成功返回已整理分类的牌
     */
    private function ispinghu($pai){
        $_push=[];
        if(!$pai)   return false;
        $_push=winkind_one($pai);
        if($_push==false) $_push=winkind_two($pai);
        if($_push==false) return false;
        return $_push;
    }
    /**
     * 流局，清空所有数据，
     * @return boolean  false | array   失败返回false，成功返回true
     */
    private function liuju(){
        $this->cards=$this->playcards=[];
        return  true;
    }
    private function getwintype($index,$hupai,$wintype){
        $hutype=array(0=>'平胡', 1=>'边倒', 2=>'对倒', 3=>'嵌倒', 4=>'单吊', 5=>'大吊', 6=>'对对胡', 7=>'杠开', 8=>'送杠', 9=>'拉杠', 10=>'天胡', 11=>'地胡', 12=>'清一色', 13=>'混一色', 14=>'字一色', 15=>'海底捞月', 16=>'八花', 17=>'四花', 18=>'中发白', 19=>'正风', 20=>'圈风', 21=>'正花', 22=>'野花', 22=>'门清', 23=>'自摸', 24=>'圈正风', 25=>'还杠', 26=>'三包', 27=>'互包');
        $hutypescore=array(0=>1, 1=>1, 2=>1, 3=>1, 4=>1, 5=>8, 6=>8, 7=>1, 8=>1, 9=>1, 10=>13, 11=>13, 12=>10, 13=>8, 14=>10, 15=>1, 16=>16, 17=>8, 18=>1, 19=>1, 20=>1, 21=>2, 22=>1, 22=>1, 23=>1, 24=>2, 25=>5, 26=>[5,4], 27=>[10,8]);
        $type=[];
        //判断是不是平胡
        if($this->playcards[$index]['pengpai']){

        }
    }
    //检查是不是平胡
    private function _checkpinghu($index,$hupai,$wintype,$winpai){
        if($this->playcards[$index]['pengpai']) return false;
        foreach($hupai as $k=>$v){
            if(count($v)==2){
                if(in_array($v[0],array(30,31,32,33))){
                    return false;
                }
            }
        }
        return true;
    }
    //检查是不是边倒
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkbiandao($index,$hupai,$wintype,$winpai){
        foreach($hupai as $k=>$v){
            if(count($v)>2){
                if(in_array($winpai,$v)){
                    if($v[0]==0  && $v[1]==1  && $v[2]==2   && $winpai==2) return true;
                    if($v[0]==10 && $v[1]==11 && $v[2]==12  && $winpai==12) return true;
                    if($v[0]==20 && $v[1]==21 && $v[2]==22  && $winpai==22) return true;
                    if($v[0]==6  && $v[1]==7  && $v[2]==8   && $winpai==6) return true;
                    if($v[0]==16 && $v[1]==17 && $v[12]==18 && $winpai==16) return true;
                    if($v[0]==26 && $v[1]==27 && $v[2]==28  && $winpai==26) return true;
                }
            }
        }
        return false;
    }
    //检查是不是对倒
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkduidao($index,$hupai,$wintype,$winpai){
        foreach($hupai as $k=>$v){
            if(count($v)>2){
                if(in_array($winpai,$v)){
                    if($v[0]==$v[1] &&  $v[1]==$v[2]) return true;
                }
            }
        }
        return false;
    }
    //检查是不是嵌倒
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkqiandao($index,$hupai,$wintype,$winpai){
        foreach($hupai as $k=>$v){
            if(count($v)>2){
                if(in_array($winpai,$v)){
                    if($v[0]+1==$winpai && $winpai+1==$v[2]) return true;
                }
            }
        }
        return false;
    }
	//检查是不是单吊
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkdandiao($index,$hupai,$wintype,$winpai){
        foreach($hupai as $k=>$v){
            if(count($v)==2){
                if(in_array($winpai,$v))  return true;
            }
        }
        return false;
    }
	//检查是不是大吊
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkdadiao($index,$hupai,$wintype,$winpai){
        if(count($hupai)==2 && in_array($winpai,$hupai)) return true;
        return false;
    }
    //检查是不是天胡
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checktianhu($index,$hupai,$wintype,$winpai){
        if(!$this->dcards) return true;
        return false;
    }
    //检查是不是地胡
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkditu($index,$hupai,$wintype,$winpai){
        if(count($this->dcards)==1) return true;
        return false;
    }
    //检查是不是清一色
    //$agangpai[0]=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkqingyise($index,$hupai,$wintype,$winpai){
		$data=[];
        foreach($this->playcards[$index]['pai'] as $k=>$v){
			 $type=$this->getMJType($v);
			 $data[$type]=$type;
			 $qingyise=$data[$type];
        }
		if(count($data)>1) return false;
		
		$data2=[];
		if($this->playcards[$index]['agangpai']){
			foreach($this->playcards[$index]['agangpai'] as $k1=>$v1){
				foreach($v1 as $k1s=>$v1s){
					 foreach($v1s as $k1sd=>$v1sd){
                          $type2=$this->getMJType($v1sd);
                          $data2[$type2]=$type2;
						  if($data2[$type2]!=$qingyise)  return false;
					}
		     	}
			}
			if(count($data2)>1) return false;
	    }
		$data3=[];
		if($this->playcards[$index]['mgangpai']){
			foreach($this->playcards[$index]['mgangpai'] as $k2=>$v2){
				foreach($v2 as $k2s=>$v2s){
					 foreach($v2s as $k2sd=>$v2sd){
						   $type3=$this->getMJType($v2sd);
                           $data3[$type3]=$type3;
						   if($data3[$type3]!=$qingyise)  return false;
					}
		     	}
			}
			if(count($data3)>1) return false;
	    }
		
		$data4=[];
		if($this->playcards[$index]['wgangpai']){
			foreach($this->playcards[$index]['wgangpai'] as $k3=>$v3){
				foreach($v3 as $k3s=>$v3s){
					 foreach($v3s as $k3sd=>$v3sd){
						   $type4=$this->getMJType($v3sd);
                           $data4[$type4]=$type4;
						   if($data4[$type4]!=$qingyise)  return false;
					}
		     	}
			}
			if(count($data4)>1) return false;
	    }
		
		$data5=[];
		if($this->playcards[$index]['pengpai']){
			foreach($this->playcards[$index]['pengpai'] as $k4=>$v4){
				foreach($v4 as $k4s=>$v4s){
					 foreach($v4s as $k4sd=>$v4sd){
						   $type5=$this->getMJType($v4sd);
                           $data5[$type5]=$type5;
						   if($data5[$type5]!=$qingyise)  return false;
					}
		     	}
			}
			if(count($data5)>1) return false;
	    }
		
		$data6=[];
		if($this->playcards[$index]['chipai']){
			foreach($this->playcards[$index]['chipai'] as $k5=>$v5){
				foreach($v5 as $k5s=>$v5s){
					 foreach($v5s as $k5sd=>$v5sd){
                           $type6=$this->getMJType($v5sd);
                           $data6[$type6]=$type6;
						   if($data6[$type6]!=$qingyise)  return false;
					}
		     	}
			}
			if(count($data6)>1) return false;
	    }
		return true;
    }
	//检查是不是混一色
    //$agangpai[0]=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkhunyise($index,$hupai,$wintype,$winpai){
		$data=[];
        foreach($this->playcards[$index]['pai'] as $k=>$v){
			 $data[]=$v;
        }
		if($this->playcards[$index]['agangpai']){
			foreach($this->playcards[$index]['agangpai'] as $k1=>$v1){
				foreach($v1 as $k1s=>$v1s){
					 foreach($v1s as $k1sd=>$v1sd){
                          $data[]=$v1sd;
					}
		     	}
			}
			
	    }
		if($this->playcards[$index]['mgangpai']){
			foreach($this->playcards[$index]['mgangpai'] as $k2=>$v2){
				foreach($v2 as $k2s=>$v2s){
					 foreach($v2s as $k2sd=>$v2sd){
                          $data[]=$v2sd;
					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['wgangpai']){
			foreach($this->playcards[$index]['wgangpai'] as $k3=>$v3){
				foreach($v3 as $k3s=>$v3s){
					 foreach($v3s as $k3sd=>$v3sd){
                          $data[]=$v3sd;
					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['pengpai']){
			foreach($this->playcards[$index]['pengpai'] as $k4=>$v4){
				foreach($v4 as $k4s=>$v4s){
					 foreach($v4s as $k4sd=>$v4sd){
                          $data[]=$v4sd;
					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['chipai']){
			foreach($this->playcards[$index]['chipai'] as $k5=>$v5){
				foreach($v5 as $k5s=>$v5s){
					 foreach($v5s as $k5sd=>$v5sd){
                          $data[]=$v5sd;
					}
		     	}
			}

	    }
		sort($data);
		//30-34
		$i=0;
		foreach($data as $ko=>$vo){
			if($vo>=34 && $vo<37){
             unset($data[$ko]);		
             $i++;			 
			}
		}
		if($i==0) return false;
		$data2=[];
		foreach($data as $k=>$v){
			 $type=$this->getMJType($v);
			 $data2[$type]=$type;
        }
		if(count($data2)>1) return false;
		return true;
    }
	//检查是不是字一色
    //$agangpai[0]=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkziyise($index,$hupai,$wintype,$winpai){
		$data=[];
        foreach($this->playcards[$index]['pai'] as $k=>$v){
			 $data[]=$v;
        }
		if($this->playcards[$index]['agangpai']){
			foreach($this->playcards[$index]['agangpai'] as $k1=>$v1){
				foreach($v1 as $k1s=>$v1s){
					 foreach($v1s as $k1sd=>$v1sd){
                          $data[]=$v1sd;
					}
		     	}
			}
			
	    }
		if($this->playcards[$index]['mgangpai']){
			foreach($this->playcards[$index]['mgangpai'] as $k2=>$v2){
				foreach($v2 as $k2s=>$v2s){
					 foreach($v2s as $k2sd=>$v2sd){
                          $data[]=$v2sd;

					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['wgangpai']){
			foreach($this->playcards[$index]['wgangpai'] as $k3=>$v3){
				foreach($v3 as $k3s=>$v3s){
					 foreach($v3s as $k3sd=>$v3sd){
                          $data[]=$v3sd;
					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['pengpai']){
			foreach($this->playcards[$index]['pengpai'] as $k4=>$v4){
				foreach($v4 as $k4s=>$v4s){
					 foreach($v4s as $k4sd=>$v4sd){
                          $data[]=$v4sd;
					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['chipai']){
			foreach($this->playcards[$index]['chipai'] as $k5=>$v5){
				foreach($v5 as $k5s=>$v5s){
					 foreach($v5s as $k5sd=>$v5sd){
                          $data[]=$v5sd;
					}
		     	}
			}

	    }
		sort($data);
		//30-34
		foreach($data as $ko=>$vo){
			if($vo>=34 && $vo<37){
             unset($data[$ko]);		
			}
		}
		if(count($data)>1) return false;
		return true;
    }
	//检查是不是海底捞月
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkhaidilaoyue($index,$hupai,$wintype,$winpai){
        if(count($this->cards['yupai'])==0) return true;
        return false;
    }
	//检查是不是八花
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkbahua($index,$hupai,$wintype,$winpai){
        if(count($this->playcards[$index]['huapai'])==8) return true;
        return false;
    }
	//检查是不是四花
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkhsihua($index,$hupai,$wintype,$winpai){
        if(in_array(40,$this->playcards[$index]['huapai']) && in_array(41,$this->playcards[$index]['huapai']) && in_array(42,$this->playcards[$index]['huapai']) && in_array(43,$this->playcards[$index]['huapai'])  ) return true;
        if(in_array(44,$this->playcards[$index]['huapai']) && in_array(45,$this->playcards[$index]['huapai']) && in_array(46,$this->playcards[$index]['huapai']) && in_array(47,$this->playcards[$index]['huapai'])  ) return true;
        return false;
    }
	//检查是不是中发白
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkzfb($index,$hupai,$wintype,$winpai){
       if($this->playcards[$index]['agangpai']){
			foreach($this->playcards[$index]['agangpai'] as $k1=>$v1){
				foreach($v1 as $k1s=>$v1s){
					 foreach($v1s as $k1sd=>$v1sd){
						  if(in_array($v1sd,array(34,35,36)))  return true;
					}
		     	}
			}
			
	    }
		if($this->playcards[$index]['mgangpai']){
			foreach($this->playcards[$index]['mgangpai'] as $k2=>$v2){
				foreach($v2 as $k2s=>$v2s){
					 foreach($v2s as $k2sd=>$v2sd){
						   if(in_array($v2sd,array(34,35,36)))  return true;

					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['wgangpai']){
			foreach($this->playcards[$index]['wgangpai'] as $k3=>$v3){
				foreach($v3 as $k3s=>$v3s){
					 foreach($v3s as $k3sd=>$v3sd){
                         if(in_array($v3sd,array(34,35,36)))  return true;
					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['pengpai']){
			foreach($this->playcards[$index]['pengpai'] as $k4=>$v4){
				foreach($v4 as $k4s=>$v4s){
					 foreach($v4s as $k4sd=>$v4sd){
                          if(in_array($v4sd,array(34,35,36)))  return true;
					}
		     	}
			}
	    }
		
    }
    //检查是不是正风位
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkzfw($index,$hupai,$wintype,$winpai){
		if($index==0) $weizhi[]=30;
		if($index==1) $weizhi[]=31;
		if($index==2) $weizhi[]=32;
		if($index==3) $weizhi[]=33;
        if($this->playcards[$index]['agangpai']){
			foreach($this->playcards[$index]['agangpai'] as $k1=>$v1){
				foreach($v1 as $k1s=>$v1s){
					 foreach($v1s as $k1sd=>$v1sd){
						  if(in_array($v1sd,$weizhi))  return true;
					}
		     	}
			}
			
	    }
		if($this->playcards[$index]['mgangpai']){
			foreach($this->playcards[$index]['mgangpai'] as $k2=>$v2){
				foreach($v2 as $k2s=>$v2s){
					 foreach($v2s as $k2sd=>$v2sd){
						   if(in_array($v2sd,$weizhi))  return true;

					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['wgangpai']){
			foreach($this->playcards[$index]['wgangpai'] as $k3=>$v3){
				foreach($v3 as $k3s=>$v3s){
					 foreach($v3s as $k3sd=>$v3sd){
                         if(in_array($v3sd,$weizhi))  return true;
					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['pengpai']){
			foreach($this->playcards[$index]['pengpai'] as $k4=>$v4){
				foreach($v4 as $k4s=>$v4s){
					 foreach($v4s as $k4sd=>$v4sd){
                          if(in_array($v4sd,$weizhi))  return true;
					}
		     	}
			}
	    }
		
    }
	//检查是不是圈风位
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkqfw($index,$hupai,$wintype,$winpai){
		if($this->cards['windarea']==0) $weizhi[]=30;
		if($this->cards['windarea']==1) $weizhi[]=31;
		if($this->cards['windarea']==2) $weizhi[]=32;
		if($this->cards['windarea']==3) $weizhi[]=33;
        if($this->playcards[$index]['agangpai']){
			foreach($this->playcards[$index]['agangpai'] as $k1=>$v1){
				foreach($v1 as $k1s=>$v1s){
					 foreach($v1s as $k1sd=>$v1sd){
						  if(in_array($v1sd,$weizhi))  return true;
					}
		     	}
			}
			
	    }
		if($this->playcards[$index]['mgangpai']){
			foreach($this->playcards[$index]['mgangpai'] as $k2=>$v2){
				foreach($v2 as $k2s=>$v2s){
					 foreach($v2s as $k2sd=>$v2sd){
						   if(in_array($v2sd,$weizhi))  return true;

					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['wgangpai']){
			foreach($this->playcards[$index]['wgangpai'] as $k3=>$v3){
				foreach($v3 as $k3s=>$v3s){
					 foreach($v3s as $k3sd=>$v3sd){
                         if(in_array($v3sd,$weizhi))  return true;
					}
		     	}
			}
	    }
		
		if($this->playcards[$index]['pengpai']){
			foreach($this->playcards[$index]['pengpai'] as $k4=>$v4){
				foreach($v4 as $k4s=>$v4s){
					 foreach($v4s as $k4sd=>$v4sd){
                          if(in_array($v4sd,$weizhi))  return true;
					}
		     	}
			}
	    }
		
    }
	//检查是不是正花
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkzhenghua($index,$hupai,$wintype,$winpai){
		$total=0;
        if($this->playcards[$index]['huapai']){
            if($index==0){
			  foreach($this->playcards[$index]['huapai'] as $k=>$v){
               if(in_array($v,array(40)))   $total++;
               if(in_array($v,array(44)))   $total++;
             }
			 
		    }
			if($index==1){
			  foreach($this->playcards[$index]['huapai'] as $k=>$v){
               if(in_array($v,array(41)))  $total++;
               if(in_array($v,array(45)))  $total++;
             }
			 
		    }
			if($index==2){
			  foreach($this->playcards[$index]['huapai'] as $k=>$v){
               if(in_array($v,array(42)))  $total++;
               if(in_array($v,array(46)))  $total++;
             }
			 
		    }
			if($index==3){
			  foreach($this->playcards[$index]['huapai'] as $k=>$v){
               if(in_array($v,array(43)))  $total++;
               if(in_array($v,array(47)))  $total++;
             }
			 
		    }
        }		
		if($total>0) return $total;
        return false;
    }
	//检查是不是野花
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkyehua($index,$hupai,$wintype,$winpai){
		$total=0;
        if($this->playcards[$index]['huapai']){
            if($index==0){
			  foreach($this->playcards[$index]['huapai'] as $k=>$v){
               if(in_array($v,array(40)))   $total++;
               if(in_array($v,array(44)))   $total++;
             }
			 
		    }
			if($index==1){
			  foreach($this->playcards[$index]['huapai'] as $k=>$v){
               if(in_array($v,array(41)))  $total++;
               if(in_array($v,array(45)))  $total++;
             }
			 
		    }
			if($index==2){
			  foreach($this->playcards[$index]['huapai'] as $k=>$v){
               if(in_array($v,array(42)))  $total++;
               if(in_array($v,array(46)))  $total++;
             }
			 
		    }
			if($index==3){
			  foreach($this->playcards[$index]['huapai'] as $k=>$v){
               if(in_array($v,array(43)))  $total++;
               if(in_array($v,array(47)))  $total++;
             }
			 
		    }
			$yutotal=count($this->playcards[$index]['huapai'])-$total;
			if($total>0) return $total;
        }		
        return false;
    }
    //检查是不是门清
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkmenqing($index,$hupai,$wintype,$winpai){
        if(count($this->playcards[$index]['pai'])==14) return true;
        return false;
    }
     //检查三包(A包B）A自摸
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checksanbaoamo($index,$hupai,$wintype,$winpai,$dindex){

        $mgangpai=$this->playcards[$data['dindex']]['mgangpai'];
        $pengpai=$this->playcards[$data['dindex']]['pengpai'];
        $chipai=$this->playcards[$data['dindex']]['chipai'];
        $num=0;
        if($mgangpai){
            foreach ($mgangpai as $k=>$v){
                if($k==$data['dindex']) $num+=count($v,0);
            }
        }
        if($pengpai){
            foreach ($pengpai as $k=>$v){
                if($k==$data['dindex']) $num+=count($v,0);
            }
        }
        if($chipai){
            foreach ($chipai as $k=>$v){
                if($k==$data['dindex']) $num+=count($v,0);
            }
        }

    }






	//检查是不是自摸
    //$hupai=array([1,2,3],[2,3,4],[2,3,4])
    private function _checkzimo($index,$hupai,$wintype,$winpai){
        if($wintype==1) return true;
        return false;
    }






}
//判断是否胡，风牌不能做将牌
function  winkind_one($cards)
{
    global $_push;
    $cards=array_values($cards);
    if (count($cards) == 2) {
        if ($cards[0] == $cards[1]  &&  $cards[0]<30) {
            $_push[]=[$cards[0],$cards[1]];
            return $_push;
        } else {
            return false;
        }
    } else {
        //从中取出一对成顺或者成刻的牌
        sort($cards);
        //再找一对成顺的牌
        $duizi=0;
        $cardsnum=array_count_values($cards);
        foreach ($cardsnum as $key => $val) {
            if($val==2) ++$duizi;
        }
        for ($i = 1; $i < count($cards) - 1; $i++) {
            if ($cards[$i - 1] + 1 == $cards[$i]  &&  (($cardsnum[$cards[$i - 1]]!=2) || $duizi>1)) {
                //从它的后面找一个比它大1的数，优先走单个顺子
                for ($j= $i + 1; $j < count($cards); $j++) {
                    if ($cards[$j] == $cards[$i] + 1)
                    {
                        //此时已经找到
                        $_push[]=[$cards[$i-1],$cards[$i],$cards[$j]];
                        unset($cards[$i-1]);
                        unset($cards[$i]);
                        unset($cards[$j]);
                        if (winkind_one($cards)) {
                            return $_push;
                        }
                        else {
                            return false;
                        }
                    }
                    else if ($cards[$j] > $cards[$i] + 1) {
                        break;
                    }
                }
            }
        }
        //先找一对成刻的牌
        for ($i = 1; $i < count($cards) - 1; $i++) {
            if ($cards[$i - 1] == $cards[$i] && $cards[$i] == $cards[$i + 1]) {
                $_push[]=[$cards[$i-1],$cards[$i],$cards[$i+1]];
                unset($cards[$i-1]);
                unset($cards[$i]);
                unset($cards[$i+1]);
                //  print_r($push);
                if (winkind_one($cards)) {
                    return $_push;
                } else {
                    break;
                }
            }
        }
        //如果刻和顺都找不到，那么说明没胡
        return false;

    }

}
//判断是否胡，风牌不能做将牌
function  winkind_two($cards)
{
    global $_push;
    $cards=array_values($cards);
    if (count($cards) == 2) {
        if ($cards[0] == $cards[1]  &&  $cards[0]<30) {
            $_push[]=[$cards[0],$cards[1]];
            return $_push;
        } else {
            return false;
        }
    } else {
        //从中取出一对成顺或者成刻的牌
        sort($cards);
        //先找一对成刻的牌
        for ($i = 1; $i < count($cards) - 1; $i++) {
            if ($cards[$i - 1] == $cards[$i] && $cards[$i] == $cards[$i + 1]) {
                $_push[]=[$cards[$i-1],$cards[$i],$cards[$i+1]];
                unset($cards[$i-1]);
                unset($cards[$i]);
                unset($cards[$i+1]);
                //  print_r($push);
                if (winkind_two($cards)) {
                    return $_push;
                } else {
                    break;
                }
            }
        }

        //再找一对成顺的牌
        $duizi=0;
        $cardsnum=array_count_values($cards);
        foreach ($cardsnum as $key => $val) {
            if($val==2) ++$duizi;
        }
        for ($i = 1; $i < count($cards) - 1; $i++) {
            if ($cards[$i - 1] + 1 == $cards[$i]  &&  (($cardsnum[$cards[$i - 1]]!=2) || $duizi>1)) {
                //从它的后面找一个比它大1的数，优先走单个顺子
                for ($j= $i + 1; $j < count($cards); $j++) {
                    if ($cards[$j] == $cards[$i] + 1)
                    {
                        //此时已经找到
                        $_push[]=[$cards[$i-1],$cards[$i],$cards[$j]];
                        unset($cards[$i-1]);
                        unset($cards[$i]);
                        unset($cards[$j]);
                        if (winkind_two($cards)) {
                            return $_push;
                        }
                        else {
                            return false;
                        }
                    }
                    else if ($cards[$j] > $cards[$i] + 1) {
                        break;
                    }
                }
            }
        }

        //如果刻和顺都找不到，那么说明没胡
        return false;

    }

}
//判断是否碰胡
function getwintype($wincards,$dapai){
    $wintype=false;
    //非赢牌排除
    if($wincards==false) return false;
    //七对排除
    if(count($wincards)==7) return false;
    //打出的牌，是否和自己的牌组成一个刻字（三张牌相同）
    if(is_array($wincards)){
        foreach($wincards as $k=>$v){
            if(in_array($dapai,$v) && count($v)==3  && $v[0]==$v[1]){
                $wintype=false;
            }
        }
    }
    return $wintype;
}



